<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/02/28
 */
namespace common\logic;

use yii;
use common\models\Fund as FundModel;
use common\models\User;

class Fund {
    /**
     * @param int $inviteCode 邀请码
     * @param int $inviter 受邀人uid
     */
    public static function inviteSend($inviteCode, $inviter) {
        if (empty($inviteCode)) return;
        $user = User::find()->where('`invite_code`=:invite_code', [':invite_code' => $inviteCode,])->one();//邀请人
        if ($user == null) {
            return [
                'err' => 1,
                'msg'=>'邀请码错误'
            ];
        }

        $transaction = Yii::$app->db->beginTransaction();
        try {
            $inviterAmount = Yii::$app->params['inviteFund'];
            $data = [
                'owner_uid' => $user['id'],
                'name' => '邀请注册潜水基金',
                'amount' => $inviterAmount,
                //'use_limit' => 1000000,
                'use_range' => FundModel::USE_RANGE_ALL,
                'status' => FundModel::STATUS_ADVANCE,
                'ctime' => time(),
                //'etime' => time() + 2592000, //1 month,暂时不考虑过期
                'ext' => json_encode([
                    'from'=>$inviter,
                ]),
            ];
            FundModel::insert($data);
            $data = [
                'owner_uid' => $inviter,
                'name' => '受邀注册潜水基金',
                'amount' => $inviterAmount,
                //单位,分
                //'use_limit' => 1000000,
                'use_range' => FundModel::USE_RANGE_ALL,
                'status' => FundModel::STATUS_CHECKED,
                'ctime' => time(),
                //'etime' => time() + 2592000,
                'ext' => json_encode([
                    'from'=>$user['id'],
                ]),
            ];
            FundModel::insert($data);
            \common\logic\User::addFundAmount($inviter, $inviterAmount);
            $transaction->commit();
            return ['err'=>0];
        } catch (\Exception $e) {
            $transaction->rollBack();
            Yii::warning($e->getMessage(), __METHOD__);
            return [
                'err' => 2,
                'msg'=>'异常'
            ];
        }
    }

    public static function getFund($uid) {
        $r = FundModel::getList("owner_uid=$uid");
        foreach ($r as $k => $v) {
            $r[$k]['ext'] = json_decode($v['ext'], true);
        }
        return $r;
    }

    /**
     * md 暂时白写了
     * @param $curUid
     * @param $id
     * @param $price
     * @return array
     */
    public static function canUseInfo($curUid, $id, $price) {
        //TODO check 范围 是否过期
        $r = FundModel::getOne("id=$id");
        if (!$r) [
            'err' => 5,
            '基金不存在'
        ];
        if($r['owner_uid']!=$curUid){
            return ['err'=>7,'msg'=>'无权限'];
        }
        switch ($r['status']) {
            case FundModel::STATUS_UNUSED:
                if ($price < $r['use_limit']) {
                    return [
                        'err' => 5,
                        'msg' => '不满' . $r['use_limit'] . '元，无法使用'
                    ];
                }
                return [
                    'err' => 0,
                    'amount' => $r['amount']
                ];
            case FundModel::STATUS_USED:
                return [
                    'err' => 1,
                    'msg' => '基金已使用',
                ];
            case FundModel::STATUS_ADVANCE:
                return [
                    'err' => 2,
                    'msg' => '不满足使用条件'
                ];
            case FundModel::STATUS_EXPIRED:
                return [
                    'err' => 3,
                    'msg' => '基金已过期'
                ];
            case FundModel::STATUS_INVALID:
                return [
                    'err' => 4,
                    'msg' => '基金无效'
                ];
                break;
        }
        return [
            'err' => 6,
            'msg' => '状态异常'
        ];
    }
}
